<template>
	<om-formsub-outer :index="index" :checkedit="checkedit">
		<om-autoform ref="autoform" :initConfigData="autoformdata" :initValue="initValue"></om-autoform>
	</om-formsub-outer>
</template>

<script>
	import {buildSubFormData, subOnLoad, changeEditor,configSelectData,hasValue} from '../../../static/js/formUtil.js';
	import { ignoreHtml,ptFloat,minusF,getDateStr} from '@/static/js/common.js';
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildSubFormData({
				zposId:"",
				posListCache:'hr-jj-tposPromotionChannel-title-poslist-cache',
				origPosCache:"hr-jj-tposPromotionChannel-title-origpos-cache",
				dataList:[]
			})
		},
		methods:{
			changeData(tree){
				for(let item of tree){
					this.dataList.push(item.attributes.pos)
					if(item.children.length){
						this.changeData(item.children)
					}
				}
			},
			getOrigPos(){
				let _that = this
				_that.om.request(
					"tposition/getComboTree"
				).then(res=>{
					_that.changeData(res)
					_that.$refs.autoform.changeEditor("title1","origPosId",{
						link:"../../public/pageSearch/pageSearch",
						"linkParam":{
							other:JSON.stringify(),
							initdata:JSON.stringify(_that.dataList),
							namefield:"instPosName",
							initload:true,
							cachename:_that.origPosCache,
							placeholder:"请输入岗位名称"
						}
					})
					if(_that.$refs.autoform.formdata.origPosId){
						let d = []
						for(let item of _that.dataList){
							if(_that.$refs.autoform.formdata.origPosId.includes(item.id)){
								d.push(item)
							}
						}
						_that.$refs.autoform.changeEditor("title1","origPosId",{
							initSelect:d
						})
						let editor = _that.$refs.autoform.getEditor("title1","origPosId")
					}
				})
			},
			getPosRequire(posId){
				let _that = this
				_that.om.request(
					"tpqInfoFiles/getPosRequire",
					{
						posId:posId
					}
				).then(res=>{
					if(res.succeed){
						 _that.$refs.autoform.formdata.require2 = res.value
					}
				})
			},
			getPosTableStr(posId){
				let _that = this
				_that.om.request(
					"tpqInfoFiles/getPosTableStr",
					{
						posId:posId
					}
				).then(res=>{
					if(res.succeed){
						 _that.$refs.autoform.formdata.require1 = res.value
					}
				})
			},
			initLoad(){
				let _that = this
				const pro1 = _that.om.request("tposPromotionChannel/getPostList",{
					zposId:_that.zposId
				})
				Promise.all([pro1]).then(res=>{
					_that.$refs.autoform.changeEditor("title1","posId",{
						link:"../../public/pageSearch/pageSearch",
						"linkParam":{
							other:JSON.stringify({}),
							defaultData:JSON.stringify(res[0]),
							cachename:_that.posListCache,
							namefield:"posname",
							initload:true,
							placeholder:"请输入岗位名称"
						}
					})
				})
			}
		},
		onShow() {
			let _that = this
			if(_that.$refs.autoform){
				uni.getStorage({
					key:_that.posListCache,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.posId = d.id
							formdata.posName = d.posname
							formdata.instName = d.instName
							formdata.posGrade = d.careercatName
							_that.getPosTableStr(d.id)
							_that.getPosRequire(d.id)
							_that.getOrigPos()
							uni.removeStorage({
								key:_that.posListCache
							})
						}
					}
				})
				uni.getStorage({
					key:_that.origPosCache,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.origPosId = d.map(e=>e.id).join(",")
							formdata.origPosName = d.map(e=>e.instPosName).join(",")
							_that.$refs.autoform.changeEditor('title1', 'origPosId', {
								initSelect: d
							})
							uni.removeStorage({
								key:_that.origPosCache
							})
						}
					}
				})
			}
		},
		onLoad(e) {
			let _that = this
			subOnLoad(_that, e, function() {
			if(e.configDataCacheKey){
				uni.getStorage({
					key: e.configDataCacheKey,
					success: function(res) {
						if(res.data && Object.prototype.toString.call(res.data)==='[object Array]'){
								_that.autoformdata = res.data
								Object.assign(_that.initValue, _that.formdata)
								_that.autoformdata[0].order = _that.order
								_that.zposId = e.posId
								changeEditor("title1","require1","disabled",true,_that.autoformdata)
								changeEditor("title1","require1","showClear",false,_that.autoformdata)
								changeEditor("title1","require1","prompt","请点击查看",_that.autoformdata)
								changeEditor("title1","require2","disabled",true,_that.autoformdata)
								changeEditor("title1","require2","showClear",false,_that.autoformdata)
								changeEditor("title1","require2","prompt","请点击查看",_that.autoformdata)
								_that.initLoad()
								_that.$nextTick(function(){
									if(_that.$refs.autoform.formdata.origPosId){
										_that.getOrigPos()
									}
								})
							}
						}
					})
				}
			})
		}
	}
</script>

<style>
</style>                                                                                                                                                                                                                                                                                                                                                                                                                                                  
